'''
Author: Sjy
Description: 等值面-均分
'''

# -*- coding: utf-8 -*-

from PyQt5 import  QtWidgets, QtCore, QtGui

class ui_isourfaceAverage(QtWidgets.QWidget):
    def setupUi(self, parent):
        parent.resize(600, 500)
        layout = QtWidgets.QVBoxLayout()
        # layout.addSpacing(20)
        # 范围
        groupRange = QtWidgets.QGroupBox('范围')
        groupRangeLayout = QtWidgets.QHBoxLayout()
        # 最小值
        self.__minValue = QtWidgets.QLineEdit()
        self.__minValue.setPlaceholderText('最小值')
        self.__minValue.setObjectName('minValue')
        self.__minValue.setValidator(QtGui.QDoubleValidator())
        self.__minValue.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        # 最大值
        self.__maxValue = QtWidgets.QLineEdit()
        self.__maxValue.setPlaceholderText('最大值')
        self.__maxValue.setObjectName('maxValue')
        self.__maxValue.setValidator(QtGui.QDoubleValidator())
        self.__maxValue.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        self._btnRefreshRange = QtWidgets.QPushButton('刷新')
        self._btnRefreshRange.setObjectName('btnRefreshRange')
        # 组范围布局内容
        groupRangeLayout.addWidget(self.__minValue)
        groupRangeLayout.addWidget(QtWidgets.QLabel(' - '))
        groupRangeLayout.addWidget(self.__maxValue)
        groupRangeLayout.addWidget(self._btnRefreshRange)
        groupRange.setLayout(groupRangeLayout)

        # 参数
        groupParam = QtWidgets.QGroupBox('设置')
        groupParamLayout = QtWidgets.QFormLayout()
        # 类型
        self._comboxType = QtWidgets.QComboBox()

        self._spinboxNum = QtWidgets.QSpinBox()
        self._spinboxPrecision = QtWidgets.QSpinBox()

        groupParamLayout.addRow('类型', self._comboxType)
        groupParamLayout.addRow('样本数', self._spinboxNum)
        groupParamLayout.addRow('精度', self._spinboxPrecision)
        groupParam.setLayout(groupParamLayout)

        # 样本数列
        groupSampleSeries = QtWidgets.QGroupBox('样本数列')
        groupSampleSeriesLayout = QtWidgets.QHBoxLayout()
        self._labelSampleSeries = QtWidgets.QLabel()
        font = QtGui.QFont()
        # 设置粗体
        font.setPointSize(12)
        font.setBold(True)
        self._labelSampleSeries.setFont(font)
        self._labelSampleSeries.setObjectName('labelSampleSeries')
        # 设置自动换行
        self._labelSampleSeries.setWordWrap(True)
        self._labelSampleSeries.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        groupSampleSeriesLayout.addWidget(self._labelSampleSeries)
        groupSampleSeries.setLayout(groupSampleSeriesLayout)

        # 底部工具行
        toolBox = QtWidgets.QHBoxLayout()
        # 生成 按钮
        btnGenerate = QtWidgets.QPushButton('生成')
        btnGenerate.setObjectName('btnGenerate')
        # 应用
        btnApply = QtWidgets.QPushButton('应用')
        btnApply.setObjectName('btnApply')
        # 确定
        btnOK = QtWidgets.QPushButton('确定')
        btnOK.setObjectName('btnOK')

        # 取消 按钮
        btnCancel = QtWidgets.QPushButton('取消')
        btnCancel.clicked.connect(parent.reject)
        toolBox.addStretch(1)
        toolBox.addWidget(btnGenerate)
        toolBox.addWidget(btnApply)
        toolBox.addWidget(btnOK)
        toolBox.addWidget(btnCancel)

        # 主容器添加内容
        layout.addWidget(groupRange)
        layout.addWidget(groupParam)
        layout.addWidget(groupSampleSeries)
        layout.addLayout(toolBox)

        # 添加容器
        parent.setLayout(layout)
        parent.setWindowTitle('生成数列')
        # 使用object name 关联槽函数
        QtCore.QMetaObject.connectSlotsByName(parent)
    
    # 获取数据
    def get(self):

        min = self.__minValue.text()
        max = self.__maxValue.text()
        if min == '':
            return '请输入最小值'
        if max == '':
            return '请输入最大值'
        sampleNum = self._spinboxNum.value()
        if sampleNum == None:
            return '样本数必须设置'

        min = float(min)
        max = float(max)
        # seriesType = self._comboxType.currentText()
        # 需要获取 checkbox 实际的值
        seriesType = self._comboxType.currentData()
        precision = self._spinboxPrecision.value()
        return {
            'min':min,
            'max':max,
            'seriesType':seriesType,
            'sampleNum':sampleNum,
            'precision':precision,
        }
    
    # 设置数据
    def set(self, data:dict={}):
        if not data:
            return
        min = data.get('min')
        if min:
            self.__minValue.setText(str(min))
        max = data.get('max')
        if max:
            self.__maxValue.setText(str(max))

    # 设置显示的 数列
    def setSeries(self, series:list=[]):
        self._labelSampleSeries.setText(', '.join(series))

    def initTypeOptions(self, options:list=[]):
        for option in options:
            self._comboxType.addItem(option.get('title'), option.get('value'))

